<?php

/**
 * giftbox actions.
 *
 * @package    sf_sandbox
 * @subpackage giftbox
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class giftboxActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeInput(sfWebRequest $request)
  {
   
  }
  
  public function executeOutput(sfWebRequest $request)
  {
        $c = new Criteria();
        $c->add(GiftboxPeer::FROM_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
        $pager = new sfPropelPager('Giftbox', sfConfig::get('app_giftbox_perpage'));
        $pager->setCriteria($c);
        $pager->setPeerMethod('doSelect');        
        $pager->setPage($this->page);
        $pager->init();
        $this->gift = $pager;   
  }
  
  
  public function executeDel(sfWebRequest $request)
  {
  	$g = GiftboxPeer::retrieveByPK($request->getParameter('id'));
  	if($g and $g->getFromId()==$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ))
  	{
  		$g->delete();
  	}	 	
    $this->getUser ()->setFlash ( 'message', 'Подарок удален' );
    $this->redirect('@giftbox_output');
  	
  }
  
  public function executePayment(sfWebRequest $request)
  {
  	$gb = GiftboxPeer::retrieveByPK($request->getParameter('id'));
  	$this->forward404Unless($gb);
  	$g = $gb->getMaterial();
  	if($g->getCost()>$this->getUser()->getProfile()->getAccount())
  	{
  	  $this->getUser ()->setFlash ( 'message', __('У вас недостаточно средств на счете!') );	
  	}
  	else
  	{
  		        $p = new Payment ( );
			$p->setLevelId ( 13 );
			$p->setSumma ( $g->getCost() );
			$p->setUserId ( $gb->getToId() );
			$p->setPaymentId ( $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$p->save();	
			$pf = $this->getUser ()->getProfile();
			$pf->setAccount($pf->getAccount()-$g->getCost());
			$pf->save();
			$gb->setPub(true);
			$gb->save();	
  		        $this->getUser ()->setFlash ( 'message', __('Оплата успешно выполнена') );
  	}
  	   $this->redirect('giftbox/output');
  }
  
  
  public function executeGive(sfWebRequest $request)
  {
  	
  	//die ('fav_id-'.$request->getParameter('fav_id').' gift_id-'.$request->getParameter('gift_id'));
  	
  	$gb = GiftboxPeer::retrieveByPK($request->getParameter('gift_id'));
  	$this->forward404Unless($gb);

  	$gb->setToId($request->getParameter('fav_id'));
  	$gb->save();

        /////платеж

  		        $p = new Payment ( );
			$p->setLevelId ( 14 );
			$p->setSumma ( $gb->getMaterial()->getCost() );
			$p->setUserId ( $request->getParameter('fav_id') );
			$p->setPaymentId ( $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
			$p->save();


        $this->redirect('giftbox/output');
   
  }  
  
  
}
